package com.example.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.pojo.Teacher;
import com.example.student.R;

import java.util.List;

public class TeacherAdapterInAddCourse  extends ArrayAdapter<Teacher> {
    private Context context;
    private List<Teacher> teachers;
    private int selectedPosition = -1; // 记录当前选中的位置

    public TeacherAdapterInAddCourse(Context context, List<Teacher> teachers) {
        super(context, R.layout.list_item_teacher, teachers);
        this.context = context;
        this.teachers = teachers;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_item_teacher, parent, false);
        }

        // 获取教师信息
        Teacher teacher = teachers.get(position);

        // 设置工号
        TextView textViewTeacherId = view.findViewById(R.id.textViewTeacherId);
        textViewTeacherId.setText("工号：" + teacher.getTeacherId());

        // 设置姓名
        TextView textViewTeacherName = view.findViewById(R.id.textViewTeacherName);
        textViewTeacherName.setText(teacher.getTeacherName());

        // 设置职位
        TextView textViewTeacherJob = view.findViewById(R.id.textViewTeacherJob);
        textViewTeacherJob.setText(teacher.getJob());

        // 根据选中状态设置背景颜色
        if (position == selectedPosition) {
            view.setBackgroundColor(Color.LTGRAY); // 选中时的背景颜色
        } else {
            view.setBackgroundColor(Color.WHITE); // 未选中时的背景颜色
        }

        return view;
    }

    // 更新选中位置
    public void setSelectedPosition(int position) {
        selectedPosition = position;
        notifyDataSetChanged();
    }

    // 获取选中位置
    public int getSelectedPosition() {
        return selectedPosition;
    }

}
